package students.StudentInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;


public class UpdateStudentFrame extends JFrame{
    JPanel formPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    JLabel jl1 = new JLabel("姓名：");//姓名
    // 创建一个JTextField，用于输入姓名
    JTextField jtf1 = new JTextField(10);

    JLabel jl2 = new JLabel("学号：");//学号
    // 创建一个JTextField，用于输入学号
    JTextField jtf2 = new JTextField(10);

    JLabel jl3 = new JLabel("性别：");//性别
    // 创建一个JComboBox，用于选择性别
    JComboBox<String> jcb3 = new JComboBox<>();

    JLabel jl4 = new JLabel("年龄：");//年龄
    // 创建一个JTextField，用于输入年龄
    JTextField jtf4 = new JTextField(10);

    JLabel jl5 = new JLabel("班级：");//班级
    // 创建一个JComboBox，用于选择班级
    JComboBox<String> jcb5 = new JComboBox<String>();

    JLabel jl6 = new JLabel("入学时间：");//入学时间
    // 创建一个JComboBox，用于选择入学时间
    JComboBox<Integer> jcb6 = new JComboBox<>();

    JLabel jl7 = new JLabel("联系电话：");//联系电话
    // 创建一个JTextField，用于输入联系电话
    JTextField jtf7 = new JTextField(10);

    // 组件行配置模板
    private void addFormRow(JPanel panel, GridBagConstraints gbc, JLabel label, Component field, int row) {
        gbc.gridy = row;
        gbc.gridx = 0;
        panel.add(label, gbc);
        gbc.gridx = 1;
        panel.add(field, gbc);
    }

    public UpdateStudentFrame(){
        super("修改学生信息");
        setSize(500, 300);
        setLocation(500, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        gbc.insets = new Insets(5,5,5,5);
        gbc.anchor = GridBagConstraints.WEST;

        // 第一行
        gbc.gridy = 0;
        formPanel.add(jl1, gbc);
        gbc.gridx = 1;
        formPanel.add(jtf1, gbc);

        // 第二行 
        gbc.gridy++;
        gbc.gridx = 0;
        formPanel.add(jl2, gbc);
        gbc.gridx = 1;
        formPanel.add(jtf2, gbc);

        // 后续行按相同模式添加...
        getContentPane().add(formPanel, BorderLayout.CENTER);
        formPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;

        addFormRow(formPanel, gbc, jl1, jtf1, 0);
        addFormRow(formPanel, gbc, jl2, jtf2, 1);
        addFormRow(formPanel, gbc, jl3, jcb3, 2);
        addFormRow(formPanel, gbc, jl4, jtf4, 3);
        addFormRow(formPanel, gbc, jl5, jcb5, 4);
        addFormRow(formPanel, gbc, jl6, jcb6, 5);
        addFormRow(formPanel, gbc, jl7, jtf7, 6);

        //姓名输入框
        jl1.setPreferredSize(new Dimension(80, 20));
        jtf1.setPreferredSize(new Dimension(150, 20));

        //学号输入框
        jl2.setPreferredSize(new Dimension(80, 20));
        jtf2.setPreferredSize(new Dimension(150, 20));

        //添加性别选项
        jcb3.addItem("男");
        jcb3.addItem("女");

        //年龄输入框
        jl4.setPreferredSize(new Dimension(80, 20));
        jtf4.setPreferredSize(new Dimension(150, 20));

        //添加班级选项
        jcb5.addItem("计科1班");
        jcb5.addItem("计科2班");
        jcb5.addItem("计科3班");
        jcb5.addItem("计科4班");
        jcb5.addItem("计科5班");
        jcb5.addItem("计科6班");

        //添加入学时间
        for(int year=2020; year<=2024; year++) {
            jcb6.addItem(year);
        }

        //联系方式输入框
        jl7.setPreferredSize(new Dimension(80, 20));
        jtf7.setPreferredSize(new Dimension(150, 20));


        //更新按钮
        JButton jb1 = new JButton("修改");
        JButton jb2 = new JButton("取消");
        //添加更新按钮事件
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户输入
                String name = jtf1.getText();
                int sno = Integer.parseInt(jtf2.getText());
                String gender = (String) jcb3.getSelectedItem();
                int age = Integer.parseInt(jtf4.getText());
                String classNo = (String) jcb5.getSelectedItem();
                int entryYear = (int) jcb6.getSelectedItem();
                String phone = jtf7.getText();
                //添加到数据库
                try {
                    //加载数据库驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    //获取数据库连接
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root", "123456");
                    //创建SQL语句
                    String sql = "insert into student values(?,?,?,?,?,?,?)";
                    //创建PreparedStatement对象
                    PreparedStatement pstmt = conn.prepareStatement(sql);
                    //设置参数
                    pstmt.setString(1, name);
                    pstmt.setInt(2, sno);
                    pstmt.setString(3, gender);
                    pstmt.setInt(4, age);
                    pstmt.setString(5, classNo);
                    pstmt.setInt(6, entryYear);
                    pstmt.setString(7, phone);
                    //执行SQL语句
                    try {
                        int affectedRows = pstmt.executeUpdate();
                        if(affectedRows > 0) {
                            JOptionPane.showMessageDialog(
                                UpdateStudentFrame.this, 
                                "成功修改" + affectedRows + "条记录",
                                "操作结果",
                                JOptionPane.INFORMATION_MESSAGE
                            );
                        } else {
                            JOptionPane.showMessageDialog(UpdateStudentFrame.this, "未修改任何记录", "提示", JOptionPane.WARNING_MESSAGE);
                        }
                    } catch (SQLException ex) {
                        JOptionPane.showMessageDialog(UpdateStudentFrame.this, "数据库异常：" + ex.getMessage(), 
                            "严重错误", JOptionPane.ERROR_MESSAGE);
                        ex.printStackTrace();
                    }
                    //关闭连接
                    pstmt.close();
                    conn.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        //添加取消按钮事件
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //关闭窗口
                dispose();
            }
        });
        //设置按钮大小
        jb1.setPreferredSize(new Dimension(100, 30));
        jb2.setPreferredSize(new Dimension(100, 30));
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(jb1);
        buttonPanel.add(Box.createRigidArea(new Dimension(20,0)));
        buttonPanel.add(jb2);
        buttonPanel.add(Box.createHorizontalGlue());
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    
        //添加面板到窗口
        add(formPanel, BorderLayout.CENTER);
    }
}
